// pages/addr_search/addr_search.js
const config = require('../../config.js');
const utils = require('../../utils/util.js');
const request = require('../../utils/request.js')
import { getMemberAddress } from '../../static/api/api.js'
const QQMapWX = require('../../sdk/qqmap-wx-jssdk.min.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageStatus:'',
        addressList:null,
        locLoading:false,
        searchData:null,
        qqMapData:null,
        searchStr:'',
        isSearch:false,
        curLocation: {
            address: '',
            poi:{
                lat:'',
                lng:''
            }
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        
        let locationData = wx.getStorageSync('locationData')
        if (locationData) {
            this.setData({
                'curLocation': locationData,
                'pageStatus':options.type ||''
            })
        }
        console.log(options)
        if (this.data.pageStatus != 'add') {
            this.getAddressList()

        }
        this.getNearAddress()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getNearAddress()
        if (this.data.pageStatus != 'add') {
            this.getAddressList()
            
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        this.setData({
            'locLoading': false
        })
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        this.setData({
            'locLoading': false
        })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    goUrl(e) {
        utils.goNavigationUrl(e)
    },
    //获取地址列表
    getAddressList() {
        let that = this
        wx.showLoading({
            title: '数据加载中...',
        })
        this.setData({
            isLoading: true
        })
        getMemberAddress().then(response => {
            console.log(response)
            wx.hideLoading()
            that.setData({
                addressList: response.data,
                isLoading: false
            })
        }).catch(response => {
            that.setData({
                isLoading: false
            })
        })

    },
    getLocation() {
        let that = this;
        
        this.setData({
            'locLoading':true
        })
        wx.getSetting({
            success(res) {
               
                if (!res.authSetting['scope.userLocation']) {
                    wx.showModal({
                        title: '温馨提示',
                        content: '该功能需要开启您的小程序定位功能，点击确定前往开启',
                        success: function (res) {
                            if (res.confirm) {
                                wx.openSetting({

                                })

                            } else if (res.cancel) {
                                that.setData({
                                    'locLoading': false
                                })
                            }

                        }
                    })
                }
            }
        })
        wx.getLocation({
            success: function (res) {
                console.log(res)
                let qqmapsdk = new QQMapWX({
                    key: config.qqMapKey
                });
                qqmapsdk.reverseGeocoder({
                    location: {
                        latitude: res.latitude,
                        longitude: res.longitude
                    },
                    // get_poi: 1,
                    // poi_options: 'policy=2;radius=3000;page_size=20;page_index=1',
                    success: function (res) {
                        console.log(res);
                        res.result.ad_info.location = res.result.location
                        let poiData ={
                            poi: res.result.ad_info,
                            address: res.result.address
                        }
                        wx.setStorageSync('locationData', poiData)
                        that.setData({
                            'curLocation': poiData
                        })
                        
                    },
                    fail: function (res) {
                        console.log(res);
                    },
                    complete:function(res){
                        that.setData({
                            'locLoading': false
                        })
                    }
                })
            },
        })
    },
    getNearAddress(){
        let that = this;
        let qqmapsdk = new QQMapWX({
            key: config.qqMapKey
        });
        let cacheData = wx.getStorageSync('locationData')
        qqmapsdk.search({
            keyword: cacheData.address,
            location:{
                latitude: cacheData.poi.location.lat,
                longitude: cacheData.poi.location.lng
            },
            page_size:3,
            success: function (res) {
                console.log(res);
                that.setData({
                    nearAddrData:res.data
                    
                })
            },
            fail: function (res) {
                console.log(res);
            }
        })
    },
    getSearchData(e) {
        let that = this;
        let qqmapsdk = new QQMapWX({
            key: config.qqMapKey
        });
        that.setData({
            'searchStr': e.detail.value
        })
        console.log(that.data.searchStr)
        qqmapsdk.search({
            keyword: '厦门市'+that.data.searchStr,
            page_size:5,
            page_index:0,
            success: function (res) {
                console.log(res);
                that.setData({
                    'qqMapData':res.data,
                    isSearch:true
                })
            },
            fail: function (res) {
                console.log(res);
            }
        })
    },
    addressItemClick(e){
        let dataset = e.currentTarget.dataset
        let poiData = {}
        if(dataset.type == 1){
            poiData = JSON.parse(dataset.item.region_name_arr)
        }else if(dataset.type == 2){
            let c = dataset.item.ad_info;
            c.location = dataset.item.location
            poiData = {
                poi: c,
                address: dataset.item.address
            } 
        }else{
            poiData = {
                poi: dataset.item.poi,
                address: dataset.item.address
            } 
        }
        
        
        if(this.data.pageStatus !='add'){
            wx.setStorageSync('locationData', poiData)
            let pages = getCurrentPages();
            let tarPage = pages[pages.length - 2];
            tarPage.setData({
                'pages.curPage': 1,
                'pages.totalPage': 1,
                'curSearchKey': '',
                'curSearchKey': '',
                'pageLoad': false,
                'listData': null,
                'isNoMore': false,
                'isScroll': false
            })
            //tarPage.getIndexList()
            wx.switchTab({
                url: '/pages/shop_index/shop_index',
            })
        }else{
            let pages = getCurrentPages();
            let tarPage = pages[pages.length - 2];
            tarPage.setData({
                curLocation: poiData,
                address: poiData.address
            })
            
            wx.navigateBack({
                
            })
        }
        
    }
})
